const service = require('../../utils/service.js');
const api = require('../../utils/util.js');
Page({
    data: {
        wayIndex: -1,
    	info:{
    		title: '转账到支付宝',
    		name: '账户名称',
    		account: '支付宝账号',
    		placeholder: '请输入支付宝账号'
    	},
    	account_name: '',    //--真实姓名
    	account_number: '',  //--账户
    	account_type: -1,    //--提现方式
    	account_money: '',   //--提现金额
    	money: 0,            //--可提现金额
    	name: '',
    	pop_isShow: false
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.init();
    },
	init: function(){
		let self = this;
		api.ajax(service.url('buyWithdraw'),{},function (res) {
			console.log("buy_withdraw： "+JSON.stringify(res));
			if(res.code == 0){
				self.setData({
					money: res.data.can_withdraw_money.toFixed(2)
				});
			}
		});
	},
	//--选择支付方式
	withdrawSelect: function(et){
		if(this.data.money == 0){
			api.toast("没有可提现金额！");
			return;
		}
		let index = +et.currentTarget.dataset.index;
		this.setData({
			wayIndex: index
		});
		if(index == 0){
			this.setData({
				info: {
					title: '确认提现到微信钱包！',
					name: '微信名称',
					account: '微信钱包',
					placeholder: '请输入微信账号',
				},
				account_type: 1
			});
		}
		if(index == 1){
			this.setData({
				info: {
					title: '转账到支付宝！',
					name: '真实姓名',
					account: '支付宝账户',
					placeholder: '请输入支付宝账号',
				},
				account_type: 2
			});
		}
		if(index == 2){
			//--银行卡
			this.openBank();
		}
	},
	//--打开银行卡提现
	openBank: function(){
		wx.navigateTo({
	      	url: '../withdraw/bank/bank?money='+this.data.money
	    })
	},
	//--打开支付宝或者微信
	openWxApliy: function(){
		this.setData({
			pop_isShow: true
		})
	},
	closeWxApliy: function(){
		this.setData({
			pop_isShow: false
		})
	},
	//--确认支付方式
	sureSelcet: function(){
		if(this.data.money == 0){
			api.toast("没有可提现金额！");
			return;
		}
		if(this.data.wayIndex == -1){
			api.toast("请选择提现方式！");
			return;
		}
		if(this.data.wayIndex == 2){
			//--银行卡
			this.openBank();
		}else{
			//--1支付宝，0微信
			this.openWxApliy();
		}
	},
	//--提交提现
	submitWithdraw: function(et){
		let self = this, values = {};
		this.setData({
			account_money: this.data.money,
			account_name: et.detail.value.accountName || '',
			account_number: et.detail.value.accountNumber || ''
		});
		values.type = this.data.account_type;
		if(this.data.account_type == 2){
			values.realname = this.data.account_name;
			values.account = this.data.account_number;
		}
		if(values.realname == '' && this.data.account_type == 2){
			api.toast("请输入"+this.data.info.name+"！");
			return;
		}
		if(values.account == '' && this.data.account_type == 2){
			api.toast(this.data.info.placeholder);
			return;
		}
		if(this.data.account_type == -1){
			api.toast('请选择提现方式！');
			return;
		}
		api.ajax(service.url('buyWithdrawApply'),values,function (res) {
			console.log("buy_withdraw_apply： "+JSON.stringify(res));
			if(res.code == 0){
				api.toast("提现申请成功，等待处理！");
				setTimeout(function(){
					wx.navigateBack({
						delta: 1
					});
				},500);
			}
		});
	}
})